﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace SpectrEngine.Framework
{
    public class DrawableGameNode
        : GameNode, IDrawableItem
    {

        public DrawableGameNode(Game game, Vector2 position, float rotation, Vector2 size)
            : base(game)
        {
            this.position = position;
            this.rotation = rotation;
            this.size = size;

            drawOrder = 0;
            visible = true;
        }

        private int drawOrder;
        public int DrawOrder
        {
            get { return drawOrder; }
            set
            {
                if (drawOrder != value)
                {
                    drawOrder = value;
                    onDrawOrderChanged();
                }
            }

        }

        protected internal void onDrawOrderChanged()
        {
            if (DrawOrderChanged != null)
            {
                DrawOrderChanged(this, EventArgs.Empty);
            }
        }

        private bool visible;
        public bool Visible
        {
            get { return visible; }
            set
            {
                if (visible != value)
                {
                    visible = value;
                    onVisibleChanged();
                }
            }
        }

        protected internal void onVisibleChanged()
        {
            if (VisibleChanged != null)
            {
                VisibleChanged(this, EventArgs.Empty);
            }
        }

        public event EventHandler DrawOrderChanged;

        public event EventHandler VisibleChanged;

        public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
        }

        protected Vector2 position;
        public Vector2 Position
        {
            get { return position; }
        }

        protected float rotation;
        public float Rotation
        {
            get { return rotation; }
        }

        protected Vector2 size;
        public Vector2 Size
        {
            get { return size; }
        }

        public Matrix World
        {
            get { return Matrix.CreateScale(size.X, size.Y, 0.0f) * Matrix.CreateRotationZ(rotation) * Matrix.CreateTranslation(position.X, position.Y, 0.0f); }
        }

        public void Translate(Vector2 value)
        {
            position += value;
        }

        public void Translate(float x, float y)
        {
            position += new Vector2(x, y);
        }

        public void Rotate(float value)
        {
            rotation = (rotation + value) % MathHelper.TwoPi;
        }

        public void Scale(Vector2 value)
        {
            size += value;
        }

        public void Scale(float x, float y)
        {
            size += new Vector2(x, y);
        }

        public event EventHandler Translated;
        public event EventHandler Rotated;
        public event EventHandler Scaled;

        protected internal virtual void onTranslated()
        {
            if (Translated != null)
            {
                Translated(this, EventArgs.Empty);
            }
        }

        protected internal virtual void onRotated()
        {
            if (Rotated != null)
            {
                Rotated(this, EventArgs.Empty);
            }
        }

        protected internal virtual void onScaled()
        {
            if (Scaled != null)
            {
                Scaled(this, EventArgs.Empty);
            }
        }
    }
}
